package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyOrderDao;
import ex.com.jm.food.entity.Attendant;
import ex.com.jm.food.entity.Order;

/**
 * 企业订单管理接口实现
 * @author wangshenfeng 
 *
 */
public class CompanyOrderDaoImpl extends HibernateDaoSupport implements
		CompanyOrderDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Order> getOrders(final int initPage,final int pageRow,final String comId,final String userName,
			final Integer payType,final Integer payStatus,final Integer orderFlage,final String s_date,final String e_date,final String order,
			final String sort) {
		// TODO Auto-generated method stub
		Object orders=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.OrderDTO("
						+ "o.orderId,"
						+ "case when o.payType=0 then '网上支付' when o.payType=1 then '餐到付款' end,"
						+ "case when o.payStatus=0 then '未支付' when o.payStatus=1 then '已支付' end,"
						+ "o.orderFlage,"
						+ "o.userName,"
						+ "o.userPhone,"
						+ "o.userAddress,"
						+ "o.userRemark,"
						+ "o.orderStartdate,"
						+ "o.orderEnddate,"
						+ "atten.attName"
						+ ") from Order o "
						+ " left join o.attId atten "
						+ "where o.comId='"+comId+"' ";
				if(userName!=null&&!userName.equals("")){
					hql+=" and o.userName like '%"+userName+"%'";
				}
				if(payType!=null&&!payType.equals("")){
					hql+=" and o.payType="+payType;
				}
				if(payStatus!=null&&!payStatus.equals("")){
					hql+=" and o.payStatus="+payStatus;
				}
				if(orderFlage!=null&&!orderFlage.equals("")){
					hql+=" and o.orderFlage="+orderFlage;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and o.orderDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and o.orderDate<='"+e_date+"'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by o."+order+" "+sort;
				}else {
					hql+=" order by o.orderDate desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Order>) orders;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int orderCount(final String comId,final String userName,final Integer payType,
			final Integer payStatus,final Integer orderFlage,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Order o where o.comId='"+comId+"' ";
				if(userName!=null&&!userName.equals("")){
					hql+=" and o.userName like '%"+userName+"%'";
				}
				if(payType!=null&&!payType.equals("")){
					hql+=" and o.payType="+payType;
				}
				if(payStatus!=null&&!payStatus.equals("")){
					hql+=" and o.payStatus="+payStatus;
				}
				if(orderFlage!=null&&!orderFlage.equals("")){
					hql+=" and o.orderFlage="+orderFlage;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and o.orderDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and o.orderDate<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateFlag(final String orderId,final Integer orderFlage) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Order o set o.orderFlage="+orderFlage+" where o.orderId='"+orderId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateOrderStartDate(final String orderId,final String date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Order o set o.orderStartdate='"+date+"' where orderId='"+orderId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@Override
	public void updateOrder(Order order) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(order);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean updateOrderEndDate(final String orderId,final String date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Order o set o.orderEnddate='"+date+"' where orderId='"+orderId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@Override
	public Order editOrder(String orderId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Order.class, orderId);
	}

	@Override
	public void delOrder(String orderId) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Order.class, orderId));
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean updateAtten(final String attId,final String orderId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="update jm_order set att_id='"+attId+"' where order_id='"+orderId+"'";
				Query query=session.createSQLQuery(sql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}


}
